package com.ruoyi.quartz.race.tool;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import com.ruoyi.quartz.race.model.RealTimeEntity;
import com.ruoyi.quartz.util.JedisUtil;
import com.ruoyi.quartz.util.SSLClient;

/**
 * 获取所有赛事及当前赛季
 */
public class RealTimeUtil {
	public static void main(String[] args) throws Exception {
		String id = "1126649";
		getHtml(id);
	}

	public static void getHtml(String id) {
		String url = "https://live.500.com/json/odds.php?fid=" + id + "&cid=3";
		// System.out.println(url);
		String text = doGet(url, "utf-8");
		text = text.trim();
		 System.out.println(text);
		if (text != null && text != "") {
			ScriptEngine js = new ScriptEngineManager().getEngineByName("JavaScript");
			String jsFunc = "(function() {" + "var ret='';" + "  var a = " + text + ";"
					+ "  for(var i=0;i<a.length;i++){" + "var b=a[i];" + "for(var j=0;j<b.length;j++){" + "var t=b[j];"
					+ "ret=ret+t[0]+','+t[1]+','+t[2]+'-'" + "}" + "}" + "  return ret;" + "})()";
			 System.out.println(jsFunc);
			try {
				String result = (String) js.eval(jsFunc);
				String[] resultarr = result.split("-");
				RealTimeEntity rte = new RealTimeEntity();
				rte.setId(id);
				rte.setBegas(resultarr[0]);
				rte.setEndas(resultarr[1]);
				rte.setBeges(resultarr[2]);
				rte.setEndes(resultarr[3]);
				rte.setBegbig(resultarr[4]);
				rte.setEndbig(resultarr[5]);
				String key = "realtime:" + id;
				JedisUtil.set(key, JSON.toJSONString(rte));
			} catch (ScriptException e) {
				RealTimeEntity rte = new RealTimeEntity();
				rte.setId(id);
				String key = "realtime:" + id;
				JedisUtil.set(key, JSON.toJSONString(rte));
			}
		}else{
			RealTimeEntity rte = new RealTimeEntity();
			rte.setId(id);
			String key = "realtime:" + id;
			JedisUtil.set(key, JSON.toJSONString(rte));
		}
	}

	private static String doGet(String url, String charset) {
		HttpClient httpClient = null;
		try {
			if (url.indexOf("https:") > -1) {
				httpClient = new SSLClient();
			} else {
				httpClient = new DefaultHttpClient();
			}
			HttpGet httpGet = new HttpGet(url);
			httpGet.setHeader("User-Agent",
					"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0");
			httpGet.addHeader("referer", "https://odds.500.com");
			httpGet.addHeader("sec-fetch-dest", "empty");
			httpGet.addHeader("sec-fetch-mode", "cors");
			httpGet.addHeader("sec-fetch-site", "same-origin");
			httpGet.addHeader("x-requested-with", "XMLHttpRequest");

			HttpResponse response = httpClient.execute(httpGet);
			int statusCode = response.getStatusLine().getStatusCode();
			if (statusCode != 200) {
				httpGet.abort();
				return "";
			}
			HttpEntity entity = response.getEntity();
			String result = null;
			if (entity != null) {
				result = EntityUtils.toString(entity, charset);
			}
			EntityUtils.consume(entity);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
}
